package com.smartleanx.module.hrm.controller.app.eagin;


import cn.hutool.json.JSONObject;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.module.hrm.service.esign.eums.ActorTypeEnum;
import com.smartleanx.module.hrm.service.esign.eums.TaskStatusEnum;
import com.smartleanx.module.hrm.service.esign.service.HrmEContractTaskService;
import com.smartleanx.module.hrm.service.esign.task.ESignTaskService;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTaskParam;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEContractTask;
import com.smartleanx.module.hrm.service.esign.temp.result.ComponentBathResult;
import com.smartleanx.module.hrm.service.esign.temp.result.ComponentResult;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmETaskListResult;
import com.smartleanx.module.hrm.service.esign.temp.vo.FillInViewPDFLinkVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.SignFlowParamVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Tag(name =  "任务")
@RestController
@RequestMapping("/hrm/esign/task/app")
public class ESignTaskAppController {

    @Autowired
    private ESignTaskService eSignTaskService;
    @Autowired
    private HrmEContractTaskService hrmEContractTaskService;

    private final Boolean debug = true;


    @Operation(summary = "获取任务列表")
    @GetMapping("/getTaskPage")

    public CommonResult<PageResult<HrmETaskListResult>> getTaskPage(HrmEContractTaskParam param) {
        return CommonResult.success(eSignTaskService.getTaskAppPage(param));
    }

    @Operation(summary = "获取填写控件")
    @GetMapping("/getFillInComponents")
    public CommonResult<ComponentResult> getFillInComponents(Long taskId, String fillInType) {
        fillInType = "2";
        HrmEContractTask hrmEContractTask = hrmEContractTaskService.getById(taskId);
        hrmEContractTask.setTaskStatus(TaskStatusEnum.AWAIT_FILL_IN_PERSON.getCode());
        hrmEContractTaskService.updateById(hrmEContractTask);
        return CommonResult.success(eSignTaskService.getFillInComponents(taskId,fillInType));
    }

    @Operation(summary = "做文件预览")
    @PostMapping("/getFillInViewToPDF")
    public CommonResult<String> getFillInComponents(@RequestBody ComponentResult param) {
        return CommonResult.success(eSignTaskService.getFillInViewToPDFApp(param));
    }

    @Operation(summary = "做文件预览详情")
    @GetMapping("/getFillInViewDetail")
    public CommonResult<String> getFillInViewDetail(Long taskId) {
        return CommonResult.success(eSignTaskService.getFillInViewDetail(taskId));
    }

    @Operation(summary = "提交填写")
    @PostMapping("/saveFillInData")
    public CommonResult<String> saveFillInData(@RequestBody FillInViewPDFLinkVo fillInViewPDFLinkVo) {
        fillInViewPDFLinkVo.setSaveType(2);
        fillInViewPDFLinkVo.setActorType("2");
        eSignTaskService.saveFillInData(fillInViewPDFLinkVo,null,null);
        return CommonResult.success("成功");
    }

    @Operation(summary = "批量提交填写")
    @PostMapping("/saveFillInDataBath")
    public CommonResult<String> saveFillInDataBath(@RequestBody ComponentBathResult param) {
        param.setFillInType(1);
        eSignTaskService.saveFillInDataBath(param);
        return CommonResult.success("成功");
    }


    @Operation(summary = "获取签署链接")
    @GetMapping("/getSignFlowLink")
    public CommonResult<String> getSignFlowLink(Long taskId,String actorType,String redirectUrl) {
        actorType = ActorTypeEnum.PERSON.getCode();
        return CommonResult.success(eSignTaskService.getSignFlowLink(taskId,actorType,redirectUrl));
    }

    @Operation(summary = "获取合同编号")
    @GetMapping("/getContractCode")
    public CommonResult getContractCode(Long tempId) {
        return CommonResult.success(eSignTaskService.getContractCode(tempId,null));
    }

    @Operation(summary = "删除签署")
    @PostMapping("/removeTask/{taskId}")
    public CommonResult removeTask(@PathVariable("taskId") Long taskId) {
        return CommonResult.success(eSignTaskService.removeTask(taskId));
    }

    @Operation(summary = "获取轮询状态")
    @GetMapping("/getTaskSignStatus")
    public CommonResult getTaskSignStatus(Long taskId) {
        return CommonResult.success(eSignTaskService.getTaskSignStatus(taskId,ActorTypeEnum.PERSON.getCode()));
    }

}
